package com.moyu.crowdsource.service;

import com.moyu.crowdsource.entity.CommodityImage;
import com.moyu.crowdsource.entity.CommodityOrder;
import com.moyu.crowdsource.entity.CommodityOrderExample;
import com.moyu.crowdsource.mapperdao.CommodityOrderMapper;
import com.moyu.crowdsource.util.Msg;
import com.moyu.crowdsource.util.MyUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class CommodityOrderService {

  @Autowired CommodityOrderMapper commodityOrderMapper;

  @Autowired CommodityImageService commodityImageService;

  public void submitOrder(CommodityOrder commodityOrder) {
    commodityOrder.setCommodityOrderId(MyUUID.getUUID32());
    commodityOrderMapper.insertSelective(commodityOrder);
  }

  public Msg submitOrderAndImg(
      CommodityOrder commodityOrder,
      MultipartFile multfile1,
      MultipartFile multfile2,
      MultipartFile multfile3,
      CommodityImage commodityImage) {
    String orderid = MyUUID.getUUID32();
    commodityOrder.setCommodityOrderId(orderid);
    commodityImage.setOrderId(orderid);
    commodityOrderMapper.insertSelective(commodityOrder);
    Msg msg = Msg.success();
    if (!commodityImageService.handleFormUploadBoolean(multfile1, commodityImage)) {
      msg.add("error1", "图片一上传失败");
    }
    if (null != multfile2) {
      if (!commodityImageService.handleFormUploadBoolean(multfile2, commodityImage)) {
        msg.add("error2", "图片二上传失败");
      }
    }
    if (null != multfile3) {
      if (!commodityImageService.handleFormUploadBoolean(multfile3, commodityImage)) {
        msg.add("error3", "图片三上传失败");
      }
    }

    return msg;
  }

  public List<CommodityOrder> getAllOrder() {
    return commodityOrderMapper.selectByExample(null);
  }

  public List<CommodityOrder> getOrderByIssueId(Integer issueUserId, String... args) {
    CommodityOrderExample example = new CommodityOrderExample();
    CommodityOrderExample.Criteria criteria = example.createCriteria();
    criteria.andIssueUserIdEqualTo(issueUserId);
    criteria.andStatusNotEqualTo("已删除");
    if (null!=args)
    for (String temp : args) {
        criteria.andStatusNotEqualTo(temp);
    }
    return commodityOrderMapper.mySelectByExample(example);
  }

  public List<CommodityOrder> getOrderByIssueIdAndStatus(Integer issueUserId, String status) {
    CommodityOrderExample example = new CommodityOrderExample();
    CommodityOrderExample.Criteria criteria = example.createCriteria();
    criteria.andIssueUserIdEqualTo(issueUserId);
    criteria.andStatusEqualTo(status);
    return commodityOrderMapper.mySelectByExample(example);
  }

  public List<CommodityOrder> getOrderByClientIdAndStatus(Integer clientUserId, String status) {
    CommodityOrderExample example = new CommodityOrderExample();
    CommodityOrderExample.Criteria criteria = example.createCriteria();
    criteria.andClientUserIdEqualTo(clientUserId);
    return commodityOrderMapper.mySelectByExample(example);
  }

  public List<CommodityOrder> getOrderConditionalQuery(
      String goodName, String variety, String status) {

    CommodityOrderExample example = new CommodityOrderExample();
    CommodityOrderExample.Criteria criteria = example.createCriteria();
    if (null != status) {
      criteria.andStatusEqualTo(status);
    }
    if (null != goodName) {

      criteria.andGoodNameLike("%" + goodName + "%");
    }
    if (null != variety) {
      criteria.andVarietyEqualTo(variety);
    }
    return commodityOrderMapper.mySelectByExample(example);
  }

  public List<CommodityOrder> getValidOrder() {
    CommodityOrderExample example = new CommodityOrderExample();
    CommodityOrderExample.Criteria criteria = example.createCriteria();
    criteria.andStatusEqualTo("已发布");
    return commodityOrderMapper.selectByExample(example);
  }

  public List<CommodityOrder> getValidOrderAndImage() {
    CommodityOrderExample example = new CommodityOrderExample();
    CommodityOrderExample.Criteria criteria = example.createCriteria();
    criteria.andStatusEqualTo("已发布");
    return commodityOrderMapper.mySelectByExample(example);
  }

  public List<CommodityOrder> getValidOrderAndImageById(String commodityOrderId) {
    CommodityOrderExample example = new CommodityOrderExample();
    CommodityOrderExample.Criteria criteria = example.createCriteria();
    criteria.andCommodityOrderIdEqualTo(commodityOrderId);
    return commodityOrderMapper.mySelectByExample(example);
  }

  public CommodityOrder getCommodityOrderByOrderId(String commodityOrderId) {
    return commodityOrderMapper.selectByPrimaryKey(commodityOrderId);
  }

  public void updateCommodityOrder(CommodityOrder commodityOrder) {
    commodityOrderMapper.updateByPrimaryKeySelective(commodityOrder);
  }

  public void delCommodityOrder(String commodityOrderId) {
    commodityOrderMapper.deleteByPrimaryKey(commodityOrderId);
  }
}
